﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Main
{
    public partial class NewCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"] != null)
            {

            }
            else
                Response.Redirect("Login.aspx");
        }

        /// <summary>
        /// triggers creation of a new customer on SQL Azure
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text != String.Empty && txtSurname.Text != String.Empty && txtPhone.Text != String.Empty && txtAddress.Text != String.Empty && txtEmail.Text != String.Empty && txtCompany.Text != String.Empty)
                {
                    ControlCommon common = new ControlCommon();
                    ControlCustomers.Control control = new ControlCustomers.Control();
                    String msg = control.CreateNewCustomer(common.ConvertToEnglish(txtName.Text.Trim()), common.ConvertToEnglish(txtSurname.Text.Trim()), common.ConvertToEnglish(txtEmail.Text.Trim().ToLower()), txtPhone.Text.Trim(), common.ConvertToEnglish(txtAddress.Text.Trim()), common.ConvertToEnglish(txtCompany.Text.Trim()), Session["Company"].ToString());

                    if (msg == String.Empty)
                    {
                        lblError.Text = "New customer has been successfully created!";
                    }
                    else
                        lblError.Text = msg;
                }
                else
                    lblError.Text = "Please give all the required information!";
            }
            catch (Exception ex)
            {
                lblError.Text = ex.ToString();
            }
        }

        /// <summary>
        /// makes all fields blank
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtName.Text = String.Empty;
            txtSurname.Text = String.Empty;
            txtEmail.Text = String.Empty;
            txtPhone.Text = String.Empty;
            txtAddress.Text = String.Empty;
            txtCompany.Text = String.Empty;
            lblError.Text = String.Empty;
        }
    }
}